from django.contrib.auth.backends import ModelBackend
from apps.users.models import User

#登陆的时候根据用户名或手机号查询用户对象
class MyAuthenticateBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        user=None

        #查询用户名
        try:
            user =User.objects.get(username=username)
        except Exception as e:
            pass

        #查询手机号
        try:
            if not user:
                user = User.objects.get(mobile=username)
        except Exception as e :
            return None

        return  user

